package com.tanhua.app.Service;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserFreezeService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 判断用户是否被冻结，已被冻结，抛出异常
     *
//     * @param state  冻结的范围 1冻结登录  2冻结发言  3冻结发布动态
     * @param userId 用户id
     */
    public void freezeCheck(Long userId,String freezingRange){
//        1.查询redis中冻结数据
        String kay = redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId);
//        2.判断冻结数据是否存在
        if (StrUtil.isNotBlank(kay)){
            Map map = JSON.parseObject(kay, Map.class);
            String RedisFreezingRange = map.get("freezingRange").toString();
//        3.如果冻结数据存在,根据冻结的范围抛异常
            if (RedisFreezingRange.equals(freezingRange)&&"1".equals(freezingRange)){
//                冻结登录
                throw new BusinessException(ErrorResult.freezeLoginError());
            }
            if (RedisFreezingRange.equals(freezingRange)&&"2".equals(freezingRange)){
//                冻结发炎
                throw new BusinessException(ErrorResult.freezeSpeakError());
            }
            if (RedisFreezingRange.equals(freezingRange)&&"3".equals(freezingRange)) {
                // 冻结发布动态
                throw new BusinessException(ErrorResult.freezeMovementError());
            }

        }



    }


}
